﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WFNetLib;
using WFNetLib.NMEA;
namespace RobotControl
{
    public partial class Form_Main : Form
    {
        HCHDT hchdt = new HCHDT();
        private void LuoJingCom_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
        {
            char rx;
            if (ComClosing[3])
                return;
            ComListening[3] = true;
            while (LuoJingCom.BytesToRead != 0)
            {
                int irx = LuoJingCom.ReadByte();
                if (irx == -1)
                    break;
                rx = (char)irx;
                if (hchdt.RxProc(rx))
                {
                    ExternAddText("收到磁罗经数据:\r\n" + hchdt.AllRx);
                    if (hchdt.True == "T")
                    {
                        if (string.IsNullOrEmpty(hchdt.Heading))
                            continue;
                        double h;
                        try
                        {
                            h = Convert.ToDouble(hchdt.Heading);
                        }
                        catch
                        {
                            continue;
                        }                        
                        double x1, x2, x;
                        x1 = Properties.Settings.Default.磁罗经修正角度;
                        if (x1 <= 180)
                        {
                            x2 = x1 + 180;
                            if (h < x1)
                            {
                                x = h - x1;
                            }
                            else if (h > x2)
                            {
                                h = h - 360;
                                x = h - x1;
                            }
                            else
                            {
                                x = h - x1;
                            }
                        }
                        else
                        {
                            x2 = x1 - 180;
                            if (h > x1)
                            {
                                x = h - x1;
                            }
                            else if (h < x2)
                            {
                                x = 360 - x1;
                                x = x + h;
                            }
                            else
                            {
                                x = x1 - h;
                            }
                        }
                        ExternChangeParamList(ParamsListAddr.航向, x.ToString("F1"));
                    }                    
                }
            }
            ComListening[3] = false;
        }
    }
}
